<?php


namespace app\common\cachemodel;


use app\common\cachemodel\live\LiveBroadcastPreview;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\cachemodel\live\LiveSubscribe;
use app\common\lib\Util;
use app\common\model\BaseCache;
use think\Cache;
use think\Env;
use XznsTools\cache\CacheManage;

/**
 * 首页按钮模型
 * Class IndexButton
 * @package app\common\cachemodel
 */
class IndexButton extends BaseCache
{
    protected $table = 'wechatadmin.system_index_button';
    protected $cacheModel = 'index.index_btns';

    protected $appType = [
        'android' => 1,
        'ios' => 2,
    ];


    protected $tipConfig = [
        1 => [ // NEW 角标
            'type' => 2,
            'icon' => '/test/index/2.9.0/new.png',
        ],
        2 => [ // 大礼包 角标
            'type' => 2,
            'icon' => '/test/index/2.9.0/gift.png',
        ],
        3 => [ // HOT 角标
            'type' => 2,
            'icon' => '/test/index/2.9.0/hot.png',
        ],
        5 => [ // 推荐 角标
            'type' => 1,
            'icon' => '/test/index/2.9.0/icon1.png',
        ],
        6 => [ // 预告 角标
            'type' => 2,
            'icon' => '/test/index/2.9.0/icon2.png',
        ],
        7 => [ // 热议中 角标
            'type' => 3,
            'icon' => '/test/index/2.9.0/icon3.png',
        ],
        8 => [ // 更新 角标
            'type' => 4,
            'icon' => '/test/index/2.9.0/icon4.png',
        ],
        9 => [ // 上新 角标
            'type' => 5,
            'icon' => '/test/index/2.9.0/icon5.png',
        ],
        10 => [ // 折扣 角标
            'type' => 6,
            'icon' => '/test/index/2.9.0/icon6.png',
        ],
        11 => [ // AI 角标
            'type' => 7,
            'icon' => '/test/index/2.9.0/icon7.png',
        ],
    ];

    protected $oldVersion = [
        '1.1.1',
        '1.3.0',
        '1.1.1',
        '1.2.1',
        '1.2.0',
        '1.2.1',
        '1.3.0',
        '1.3.1',
        '1.3.2',
        '1.4.0',
        '1.4.1',
        '1.4.2',
        '1.4.3',
        '1.5.0',
        '1.5.1',
        '1.5.2',
        '1.6.0',
        '1.6.1',
        '1.6.2',
        '1.7.0',
        '1.7.1',
        '1.7.2',
        '1.8.0',
        '1.8.1',
        '1.9.0',
        '1.9.1',
        '1.9.2',
    ];

    /**
     * 获取首页按钮缓存
     * @param array $map
     * @param bool $isNew 是不是新版本 1.9.4以上的版本都是
     * @param bool $isYoung 是不是青少年模式
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getIndexButton($map = [], $isNew = false, $isYoung = false)
    {
        if (!isset($map['ver'], $map['app_type']) || empty($map))
        {
            return [];
        }
        $ver = $map['ver'];
        // 0=>老版本,1=>andriod,2=>ios
        $app_type = in_array($ver, $this->oldVersion) ? 0 : ($this->appType[$map['app_type']] ?? 0);

        $idKey = $ver . ':' . $app_type;
        //缓存中获取首页按钮数据
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->getCache();
        if (!$data)
        {
            //数据库中获取首页按钮数据
            $indexButton = $this->getIndexButtonByDb($ver, $app_type);

            if (!$indexButton) return [];
            //生成首页按钮缓存数据
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->setCache($indexButton, 'details', false);
            $data = $indexButton;
        } else
        {
            $data = json_decode($data, true);
            if (!isset($data[0]['link'], $data[0]['young_link'], $data[0]['target']))
            {
                $indexButton = $this->getIndexButtonByDb($ver, $app_type);

                if (!$indexButton) return [];

                CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->setCache($indexButton, 'details', false);
                $data = $indexButton;
            }
        }

        $data = is_array($data) ? $data : json_decode($data, true);

        foreach ($data as $key => $value)
        {
            if ($isNew)
            {   // 1.9.5版本及以上数据结构
                $value['link'] = htmlspecialchars_decode($value['link']);
                unset($data[$key]['type']);
            } else
            {   // 旧版本数据结构
                unset($data[$key]['target'], $data[$key]['link']);
            }
            if ($isYoung)
            {
                $value['link'] = $value['young_link'] ?? '';
            }
            unset($data[$key]['young_link']);

            if (Util::CheckAppVersion('2.9.0', $ver) === '小')
            {
                unset($value['photo']);
            }

            $data[$key] = $value;
        }

        return $data;
    }

    /**
     * 获取首页按钮缓存
     * @param array $map
     * @param string $type 按钮type
     * @param bool $isYoung 是不是青少年模式
     * @return int|null
     * @throws \Exception
     */
    public function getIndexButtonYoungLink($map = [], string $type, $isYoung = false)
    {
        if (!isset($map['ver'], $map['app_type']) || empty($map))
        {
            return [];
        }
        $ver = $map['ver'];
        // 0=>老版本,1=>andriod,2=>ios
        $app_type = in_array($ver, $this->oldVersion) ? 0 : ($this->appType[$map['app_type']] ?? 0);

        $idKey = $ver . ':' . $app_type;
        //缓存中获取首页按钮数据
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->getCache();
        if (!$data)
        {
            //数据库中获取首页按钮数据
            $indexButton = $this->getIndexButtonByDb($ver, $app_type);

            if (!$indexButton) return [];
            //生成首页按钮缓存数据
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->setCache($indexButton, 'details', false);
            $data = $indexButton;
        } else
        {
            $data = json_decode($data, true);
            if (!isset($data[0]['link'], $data[0]['young_link'], $data[0]['target']))
            {
                $indexButton = $this->getIndexButtonByDb($ver, $app_type);

                if (!$indexButton) return [];

                CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->setCache($indexButton, 'details', false);
                $data = $indexButton;
            }
        }

        $data = is_array($data) ? $data : json_decode($data, true);

        $needAuth = null;
        foreach ($data as $key => $value)
        {
            if ($type === $value['type'])
            {
                $link = $isYoung ? $value['young_link'] : $value['link'];
                $urlData = parse_url($link);
                if (isset($urlData['query']))
                {
                    $queryParts = explode('&', $urlData['query']);
                    $params = [];
                    foreach ($queryParts as $param)
                    {
                        $item = explode('=', $param);
                        $params[$item[0]] = $item[1];
                    }
                    $needAuth = $params['need_auth'];
                }
            }
        }
        return (int)$needAuth;
    }

    /**
     * 从数据库中获取版本按钮bid
     * @param $ver
     * @param $app_type
     * @return mixed|string
     */
    protected function getIndexButtonByDb($ver, $app_type = 0)
    {
        try
        {
            $btnCat = (new IndexButtonCat())->getIndexButtonCat(['ver' => $ver, 'app_type' => $app_type]);
            $btnCat = unserialize($btnCat);

            $field = 'name,icon,type,is_new,target,link,young_link,photo';

            $data = $this->field($field)->where(['id' => ['in', $btnCat]])->order('sort DESC')->selectOrFail();

            $data = json_decode(json_encode($data), true);

            foreach ($data as $key => $item)
            {
                $data[$key]['link'] = htmlspecialchars_decode($item['link']);
                $data[$key]['young_link'] = htmlspecialchars_decode($item['young_link']);
                $data[$key]['photo'] = isset($item['photo']) && $item['photo'] ? json_decode($item['photo'], true) : [];
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 获取首页按钮
     * @param array $map
     * @param bool $isNew
     * @param bool $isYoung
     * @param bool $isAppleCheck 是否是苹果审核
     * @return array
     * @throws \Exception
     */
    public function getIndexButtonV2($map = [], $isNew = false, $isYoung = false, bool $isAppleCheck = false): array
    {
        $cd = Cache::get('polardb_cpu_avg_per_min');

        if ($cd)
        {
            $cd = json_decode($cd, true);
            if ($cd['is_polardb_enable'] === 1)
            {
                $rid = isset($map['userinfo']['rid']) ? (int)$map['userinfo']['rid'] : 0;
                $uid = isset($map['userinfo']['id']) ? (int)$map['userinfo']['id'] : 0;
            } else
            {
                $rid = 0;
                $uid = 0;
            }
        } else
        {
            $rid = 0;
            $uid = 0;
        }

        $btns = $this->getIndexButton($map, $isNew, $isYoung);

        $btnData = array_column($btns, null, 'type');
        $resBtn1 = [];
        $isLiveIng = false;
        $liveNative = 1;//1=>原生 0=>H5
        $preview = false;
        if (Util::CheckAppVersion('2.9.6', $map['ver']) === '小')
        {   // 2.9.6 版本之前
            // 判断是否购买过一生报告
            if ($rid)
            {
                $recodeModel = new \app\common\model\Recode();
                $recodeInfo = $recodeModel->getRecode(['uid' => $uid, 'id' => $rid]);
                $natalReportData = (new NatalReport())->getNatalReport2($recodeInfo);
                $natalReportUnlock = $natalReportData ? true : false;
            } else
            {
                $natalReportUnlock = false;
            }

            if ($natalReportUnlock)
            {   // 解锁过
                $resBtn1[0] = [
                    'img' => $btnData['relation_analysis']['photo']['photo1']['url'] ?? '',
                    'target' => $btnData['relation_analysis']['target'] ?? '',
                    'link' => $btnData['relation_analysis']['link'] ?? '',
                ];
                $resBtn1[2] = [
                    'img' => $btnData['natal_report']['photo']['photo3']['url'] ?? '',
                    'target' => $btnData['natal_report']['target'] ?? '',
                    'link' => $btnData['natal_report']['link'] ?? '',
                ];
            } else
            {   // 未解锁过
                $resBtn1[0] = [
                    'img' => $btnData['natal_report']['photo']['photo1']['url'] ?? '',
                    'target' => $btnData['natal_report']['target'] ?? '',
                    'link' => $btnData['natal_report']['link'] ?? '',
                ];
                $resBtn1[2] = [
                    'img' => $btnData['relation_analysis']['photo']['photo3']['url'] ?? '',
                    'target' => $btnData['relation_analysis']['target'] ?? '',
                    'link' => $btnData['relation_analysis']['link'] ?? '',
                ];
            }
            unset($btnData['natal_report'], $btnData['relation_analysis']);

            $dateInt = (int)date('md');
            $dayInt = (int)date('d');
            if ($dateInt >= 1205 || $dateInt <= 315)
            {   // 年运推荐范围
                $yearUnlock = $rid > 0 ? (new YearFortune())->getReocdeFortuneUnlockYears($rid) : [];
                $nextYear = $dateInt > 1205 ? (int)date('Y', strtotime('+1 year')) : (int)date('Y');
                if (empty($yearUnlock) || !in_array($nextYear, $yearUnlock, false))
                {
                    $resBtn1[1] = [
                        'img' => $btnData['fortune_for_year']['photo']['photo2']['url'] ?? '',
                        'target' => $btnData['fortune_for_year']['target'] ?? '',
                        'link' => 'fortune_for_year?rid=' . $rid . '&time=' . $nextYear . '&need_auth=' . ($isYoung ? 3 : 1),
                    ];
                }
            }
            if (!isset($resBtn1[1]) && ($dayInt >= 25 || $dayInt <= 5))
            {   // 月运推荐 获取用户已解锁的数据
                $unlockMonths = $rid > 0 ? (new MonthFortune())->getReocdeFortuneUnlockMonths($rid) : [];
                $nextMonth = $dayInt >= 25 ? date('Y-m', strtotime('+1 month')) : date('Y-m');
                if (empty($unlockMonths) || !in_array($nextMonth, $unlockMonths, true))
                {
                    $resBtn1[1] = [
                        'img' => $btnData['fortune_for_month']['photo']['photo2']['url'] ?? '',
                        'target' => $btnData['fortune_for_month']['target'] ?? '',
                        'link' => 'fortune_for_month?rid=' . $rid . '&time=' . $nextMonth . '&need_auth=' . ($isYoung ? 3 : 1),
                    ];
                }
            }
            unset($btnData['fortune_for_year'], $btnData['fortune_for_month']);
            if (!isset($resBtn1[1]))
            {
                $resBtn1[1] = [
                    'img' => $btnData['question_answer']['photo']['photo2']['url'] ?? '',
                    'target' => $btnData['question_answer']['target'] ?? '',
                    'link' => $btnData['question_answer']['link'] ?? '',
                ];
                unset($btnData['question_answer']);
            }

            // 判断当前直播
            if (!$isAppleCheck)
            {   // 不是苹果审核模式
                //获取直播预告信息
                $preview = (new LiveBroadcastPreview())->getNewestPreview();
                if ($preview)
                {
                    $preview['record_id'] = (new LiveRecordingInfo())->getNewestRecord($preview['live_id']);
                }
                $recordId = (new LiveRecordingInfo())->getNewestRecord(0);
                $record = (new LiveRecordingInfo())->getInfoById($recordId);
                //直播中
                if (isset($record['status']) && (int)$record['status'] === 1)
                {
                    $isLiveIng = true;
                    $resBtn1[3] = [
                        'img' => '/test/index/2.9.0/live_ing.gif',
                        'target' => 'local',
                        'link' => "live_web?live_id={$record['live_id']}&live_record_id={$record['id']}&live_native={$liveNative}&need_auth=" . ($isYoung ? 3 : 0),
                    ];
                    unset($btnData['live']);
                } else if ($preview)
                {
                    $timestamp = time();
                    $three_timestamp = $preview['live_start_time'] - (3600 * 24);
                    if ($preview['live_start_time'] > $timestamp && $three_timestamp < $timestamp)
                    {
                        $resBtn1[3] = [
                            'img' => '/test/index/2.9.0/live_preview.png',
                            'target' => 'local',
                            'link' => "live_list_page?need_auth=" . ($isYoung ? 3 : 0),
                            // 'link' => "live_web?live_id={$preview['live_id']}&live_record_id=0&live_native=0&need_auth=" . ($isYoung ? 3 : 0),
                        ];
                        unset($btnData['live']);
                    }
                    // //判断直播是否已开启
                    // $record = (new LiveRecordingInfo())->getInfoById($preview['record_id']);
                    // //直播中
                    // if (isset($record['status']) && (int)$record['status'] === 1)
                    // {
                    //     $isLiveIng = true;
                    //     $resBtn1[3] = [
                    //         'img' => '/test/index/2.9.0/live_ing.gif',
                    //         'target' => 'local',
                    //         'link' => "live_web?live_id={$preview['live_id']}&live_record_id={$preview['record_id']}&live_native={$liveNative}&need_auth=0",
                    //     ];
                    // }
                }
            } else
            {   // 苹果审核模式
                unset($btnData['custom_report'], $btnData['mental_test_list']);
            }
            if (!isset($resBtn1[3]) && isset($btnData['question_answer']))
            {
                $resBtn1[3] = [
                    'img' => $btnData['question_answer']['photo']['photo3']['url'] ?? '',
                    'target' => $btnData['question_answer']['target'] ?? '',
                    'link' => $btnData['question_answer']['link'] ?? '',
                ];
                unset($btnData['question_answer']);
            }

            $usePrice = $uid > 0 ? (new Orders())->getUserTotalAmount($uid) : 0;

            if (!isset($resBtn1[3]) && $usePrice >= 300 && !$isAppleCheck)
            {
                $resBtn1[3] = [
                    'img' => $btnData['custom_report']['photo']['photo3']['url'] ?? '',
                    'target' => $btnData['custom_report']['target'] ?? '',
                    'link' => $btnData['custom_report']['link'] ?? '',
                ];
                unset($btnData['custom_report']);
            }
            if (!isset($resBtn1[3]) && $usePrice >= 200)
            {
                $unlockAcgMap = $rid > 0 ? (new Orders())->PrepaidUnpaidOrderIds($uid, $rid, 5, 1) : [];
                if ($unlockAcgMap)
                {
                    $resBtn1[3] = [
                        'img' => $btnData['acg_map']['photo']['photo3']['url'] ?? '',
                        'target' => $btnData['acg_map']['target'] ?? '',
                        'link' => $btnData['acg_map']['link'] ?? '',
                    ];
                    unset($btnData['acg_map']);
                }
            }
            if (!isset($resBtn1[3]))
            {
                $resBtn1[3] = [
                    'img' => $btnData['astrolable']['photo']['photo3']['url'] ?? '',
                    'target' => $btnData['astrolable']['target'] ?? '',
                    'link' => $btnData['astrolable']['link'] ?? '',
                ];
                unset($btnData['astrolable']);
            }

            ksort($resBtn1);
        } else
        {
            // 判断当前直播
            if (!$isAppleCheck)
            {   // 不是苹果审核模式
                //获取直播预告信息
                $preview = (new LiveBroadcastPreview())->getNewestPreview();
                if ($preview)
                {
                    $preview['record_id'] = (new LiveRecordingInfo())->getNewestRecord($preview['live_id']);
                }
                $recordId = (new LiveRecordingInfo())->getNewestRecord(0);
                $record = (new LiveRecordingInfo())->getInfoById($recordId);
                //直播中
                if (isset($record['status']) && (int)$record['status'] === 1)
                {
                    $isLiveIng = true;
                }
                $resBtn1Num4 = [
                    'img' => $isLiveIng ? '/test/index/2.9.0/live_ing.gif' : '/test/button/live_preview.png',
                    'target' => 'local',
                    'link' => 'live_list_page?need_auth=' . ($isYoung ? 3 : 0),
                ];
            } else
            {   // 苹果审核模式  删除定制报告   删除心理测试  删除星盘
                if (isset($btnData['astrolable']))
                {
                    $resBtn1Num4 = [
                        'img' => $btnData['astrolable']['photo']['photo3']['url'] ?? '',
                        'target' => $btnData['astrolable']['target'] ?? '',
                        'link' => $btnData['astrolable']['link'] ?? '',
                    ];
                }
                unset($btnData['custom_report'], $btnData['mental_test_list'], $btnData['astrolable']);
            }
            if (Util::CheckAppVersion('2.9.7', $map['ver']) === '大')
            {
                // 2.9.8 版本 写死跳转
                if (Util::IsHuaweiAndOppo() && Util::CheckAppVersion('2.9.10', $map['ver']) === '大')
                {   // 华为
                    $resBtn1 = [
                        [
                            'img' => '/test/button/astro4.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [
                            'img' => '/test/button/natal_report3.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [
                            'img' => '/test/button/horoscope_read3.png',
                            'target' => 'local',
                            'link' => 'horoscope_analysis_info?tab_index=1&need_auth=' . ($isYoung ? 3 : 1),
                        ],
                    ];
                } else
                {
                    $resBtn1 = [
                        [
                            // 不合规的星盘也要修改为星图
                            'img' => Util::CheckAppVersion('2.9.13') !== '小' ? '/test/button/astro4.png' : '/test/button/astro.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [
//                    'img' => '/test/button/natal_report.png',
                            'img' => '/test/button/natal_report1.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [
                            'img' => '/test/button/horoscope_read.png',
                            'target' => 'local',
                            'link' => 'horoscope_analysis_info?tab_index=1&need_auth=' . ($isYoung ? 3 : 1),
                        ],
                    ];
                }
            } else
            {
                // 2.9.6 版本 写死跳转
                $resBtn1 = [
                    [
                        'img' => Util::IsHuawei() ? '/test/button/horoscope_read_info3.png' : '/test/button/horoscope_read_info.png',
                        'target' => 'local',
                        'link' => 'horoscope_analysis_info?tab_index=1&need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [
//                    'img' => '/test/button/natal_report.png',
                        'img' => '/test/button/natal_report1.png',
                        'target' => 'local',
                        'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [
                        'img' => '/test/button/relation_analysis.png',
                        'target' => 'local',
                        'link' => 'relation_research_home?need_auth=' . ($isYoung ? 3 : 0),
                    ],
                ];
            }
            if ($resBtn1Num4)
            {
                $resBtn1[] = $resBtn1Num4;
            }
        }

        // 计算推荐的 按钮类型
        $recommentType = '';
        if ($uid === 0)
        {   // 未登录
            $recommentType = 'rectification';
        } else
        {
            $regTime = isset($map['userinfo']['reg_time']) ? (int)$map['userinfo']['reg_time'] : 0;

            if ($rid === 0)
            {
                if (time() - $regTime < 86400 * 30)
                {   // 注册一个月内
                    $recommentType = 'rectification';
                } else
                {
                    $recommentType = 'acg_map'; // 幸运之城
                }
            } else
            {
                $unlockRectification = (new Orders())->PrepaidUnpaidOrderIds($uid, $rid, 1, 1);
                if ($unlockRectification)
                {   // 已经解锁了 生时矫正
                    $unlockAcgMap = $rid > 0 ? (new Orders())->PrepaidUnpaidOrderIds($uid, $rid, 5, 1) : [];
                    if ($unlockAcgMap)
                    {
                        $recommentType = 'custom_report'; // 定制报告
                    } else
                    {
                        $recommentType = 'acg_map'; // 幸运之城
                    }
                } else
                {
                    if (time() - $regTime < 86400 * 30)
                    {
                        $recommentType = 'rectification';
                    } else
                    {
                        $unlockAcgMap = $rid > 0 ? (new Orders())->PrepaidUnpaidOrderIds($uid, $rid, 5, 1) : [];
                        if ($unlockAcgMap)
                        {
                            $recommentType = 'custom_report'; // 定制报告
                        } else
                        {
                            $recommentType = 'acg_map'; // 幸运之城
                        }
                    }
                }
            }
        }

        $resBtn2 = [];
        foreach ($btnData as $type => $btnDatum)
        {
            switch ($btnDatum['is_new'])
            {
                case 4:
                    if ($recommentType === $type)
                    {
                        $tipIcon = $this->tipConfig[5]['icon'];
                        $tipIconType = $this->tipConfig[5]['type'];
                    } else
                    {
                        $tipIcon = '';
                        $tipIconType = 0;
                    }
                    break;
                case 1:
                case 2:
                case 3:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:// AI
                    $tipIcon = $this->tipConfig[$btnDatum['is_new']]['icon'];
                    $tipIconType = $this->tipConfig[$btnDatum['is_new']]['type'];
                    break;
                default:
                    $tipIcon = '';
                    $tipIconType = 0;
            }

            if (Util::IsHuawei() && $btnDatum['name'] === '八字排盘')
            {
                $btnDatum['name'] = '生辰';
            }

            $resBtn2[] = [
                'name' => $btnDatum['name'],
                'icon' => $btnDatum['icon'],
                'target' => $btnDatum['target'],
                'link' => $btnDatum['link'],
                'tip_icon' => $tipIcon,
                'tip_icon_type' => $tipIconType,
                'type' => $btnDatum['type'],
            ];
        }
        $isFilterShow = true; // 是否过滤显示  true 过滤显示指定按钮  false 过滤不显示指定按钮
        if (Util::AppIsPassShowV1())
        {   // 合规
            if (Util::CheckAppVersion('2.9.12') === '同')
            {
                $filterBtn = ['divination_question', 'identify_love', 'fortune_day_note', 'day', 'course_list', 'horoscope_analysis_info'];
                $resBtn1 = [
                    [   // 合盘
                        'img' => '/test/button/composite_max.png',
                        'target' => 'local',
                        'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 星话题
                        'img' => '/test/button/astr_article.png',
                        'target' => 'local',
                        'link' => 'topic_list_page?need_auth=0',
                    ],
                    [   // 视频星文
                        'img' => '/test/button/video_article.png',
                        'target' => 'local',
                        'link' => 'video_and_article_list?need_auth=0',
                    ],
                ];
                $recordId = (new LiveRecordingInfo())->getNewestRecord(0);
                $record = (new LiveRecordingInfo())->getInfoById($recordId);
                //直播中
                if (isset($record['status']) && (int)$record['status'] === 1)
                {
                    $isLiveIng = true;
                    $resBtn1[3] = [
                        'img' => '/test/index/2.9.0/live_ing.gif',
                        'target' => 'local',
                        'link' => "live_web?live_id={$record['live_id']}&live_record_id={$record['id']}&live_native={$liveNative}&need_auth=" . ($isYoung ? 3 : 0),
                    ];
                } else
                {
                    $resBtn1[3] = [
                        'img' => '/test/index/2.9.0/live_preview.png',
                        'target' => 'local',
                        'link' => "live_list_page?need_auth=" . ($isYoung ? 3 : 0),
                    ];
                }
            } elseif (Util::CheckAppVersion('2.10.0') !== '小')
            {
                if (Util::IsHuawei())
                {
                    $filterBtn = ['divination_question', 'identify_love', 'fortune_day_note', 'day', 'mental_test_list', 'horoscope_analysis_info', 'live', 'topic', 'articles'];
                } else
                {
                    $filterBtn = ['divination_question', 'identify_love', 'fortune_day_note', 'day', 'course_list', 'horoscope_analysis_info', 'live', 'topic', 'articles'];
                }
                $resBtn1 = [
                    [   // 星图
                        'img' => '/test/button/astro5.png',
                        'target' => 'local',
                        'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 合盘
                        'img' => Util::IsHuawei() ? '/test/button/composite3.png' : '/test/button/composite2.png',
                        'target' => 'local',
                        'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 冥想疗愈
                        'img' => '/test/button/meditate.png',
                        'target' => 'local',
                        'link' => 'meditate_list?type=0&need_auth=0',
                    ],
                ];
            } else
            {
                $filterBtn = ['divination_question', 'identify_love', 'fortune_day_note', 'day', 'course_list', 'horoscope_analysis_info', 'live'];
                $resBtn1 = [
                    [   // 星图
                        'img' => '/test/button/astro4.png',
                        'target' => 'local',
                        'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 合盘
                        'img' => '/test/button/composite2.png',
                        'target' => 'local',
                        'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 星话题
                        'img' => '/test/button/astr_article1.png',
                        'target' => 'local',
                        'link' => 'topic_list_page?need_auth=0',
                    ],
                    [   // 视频星文
                        'img' => '/test/button/video_article.png',
                        'target' => 'local',
                        'link' => 'video_and_article_list?need_auth=0',
                    ],
                ];
            }
            if (Util::CheckAppVersion('2.13.0') !== '小')
            {
                if (Util::IsHuaweiOrHonor())
                {
                    $filterBtn = ['divination_question', 'meditate_list', 'identify_love', 'fortune_day_note', 'day', 'mental_test_list', 'horoscope_analysis_info', 'topic', 'articles'];
                    $resBtn1 = [
                        [   // 星图
                            'img' => '/test/button/astro5.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 合盘
                            'img' => '/test/button/composite3.png',
                            'target' => 'local',
                            'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 后天星座
                            'img' => '/test/button/acquired_astro_btn.png',
                            'target' => 'local',
                            'link' => 'acquired_astro_index?pay_suc=0&need_auth=0',
                        ],
                    ];
                } elseif (Util::IsOppo())
                {
                    $isFilterShow = false;
                    // 不显示  八字排盘 、 定制报告  、  吉日吉时
                    $filterBtn = ['horoscope_home', 'custom_report', 'horoscope_select_time_index', 'horoscope_name_index'];

                    $resBtn1 = [
                        [   // 星图
                            'img' => '/test/button/astro5.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 一生报告
                            'img' => '/test/button/natal_report4.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 后天星座
                            'img' => '/test/button/acquired_astro_btn.png',
                            'target' => 'local',
                            'link' => 'acquired_astro_index?pay_suc=0&need_auth=0',
                        ],
                    ];
                } else
                {
                    $filterBtn = ['divination_question', 'meditate_list', 'identify_love', 'fortune_day_note', 'day', 'course_list', 'horoscope_analysis_info', 'live', 'topic', 'articles'];
                    $resBtn1 = [
                        [   // 星图
                            'img' => '/test/button/astro5.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 一生报告
                            'img' => '/test/button/natal_report4.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 后天星座
                            'img' => '/test/button/acquired_astro_btn.png',
                            'target' => 'local',
                            'link' => 'acquired_astro_index?pay_suc=0&need_auth=0',
                        ],
                    ];
                }
            }
        } else
        {   // 不合规情况
            // if (Util::IsHuaweiAndOppo() && !Util::huaweiIsShowAll())
            if (Util::IsHuawei() && !Util::huaweiIsShowAll())
            {
                if (Util::IsHuawei() && Util::CheckAppVersion('2.9.11') !== '小')
                {
                    $filterBtn = ['acg_map', 'divination_question', 'rectification', 'identify_love', 'day', 'articles', 'topic', 'course_list', 'horoscope_analysis_info'];
                    $resBtn1 = [
                        [   // 关系研究所
                            'img' => '/test/button/relation_analysis1.png',
                            'target' => 'local',
                            'link' => 'relation_research_home?need_auth=0',
                        ],
                        [   // 一生报告
                            'img' => '/test/button/natal_report3.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 心理
                            'img' => '/test/button/mental_test.png',
                            'target' => 'local',
                            'link' => 'mental_test_list?page=mental_test_list&cid=0&sort=0&need_auth=' . ($isYoung ? 3 : 2),
                        ],
                        [   // 合盘
                            'img' => '/test/button/composite.png',
                            'target' => 'local',
                            'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                    ];
                } else
                {
                    $filterBtn = ['acg_map', 'divination_question', 'rectification', 'identify_love', 'day', 'articles', 'topic', 'course_list', 'horoscope_analysis_info', 'relation_analysis'];
                    $resBtn1 = [
                        [
                            'img' => '/test/button/astro4.png',
                            'target' => 'local',
                            'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 一生报告
                            'img' => '/test/button/natal_report3.png',
                            'target' => 'local',
                            'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                        [   // 心理
                            'img' => '/test/button/mental_test.png',
                            'target' => 'local',
                            'link' => 'mental_test_list?page=mental_test_list&cid=0&sort=0&need_auth=' . ($isYoung ? 3 : 2),
                        ],
                        [   // 合盘
                            'img' => '/test/button/composite.png',
                            'target' => 'local',
                            'link' => 'composite?need_auth=' . ($isYoung ? 3 : 1),
                        ],
                    ];
                }
            }

            if (Util::CheckAppVersion('2.13.0') !== '小')
            {
                $resBtn1 = [
                    [   // 星图
                        'img' => '/test/button/astro5.png',
                        'target' => 'local',
                        'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 一生报告
                        'img' => '/test/button/natal_report4.png',
                        'target' => 'local',
                        'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 后天星座
                        'img' => '/test/button/acquired_astro_btn.png',
                        'target' => 'local',
                        'link' => 'acquired_astro_index?pay_suc=0&need_auth=0',
                    ],
                ];
            } elseif (Util::CheckAppVersion('2.10.0') !== '小')
            {
                $resBtn1 = [
                    [   // 星图
                        'img' => '/test/button/astro5.png',
                        'target' => 'local',
                        'link' => 'astrolable?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 一生报告
                        'img' => '/test/button/natal_report4.png',
                        'target' => 'local',
                        'link' => 'report_index?need_auth=' . ($isYoung ? 3 : 1),
                    ],
                    [   // 冥想疗愈
                        'img' => '/test/button/meditate.png',
                        'target' => 'local',
                        'link' => 'meditate_list?type=0&need_auth=0',
                    ],
                ];
            }
        }

        if (isset($filterBtn))
        {
            $bt = [];
            foreach ($resBtn2 as $item)
            {
                if (($isFilterShow && in_array($item['type'], $filterBtn, true)) || (!$isFilterShow && !in_array($item['type'], $filterBtn, true)))
                {
                    if ($item['type'] === 'fortune_day_note')
                    {
                        $item['name'] = '每日笔记';
                    }
                    if ($item['type'] === 'day')
                    {
                        $item['name'] = '每日海报';
                    }
                    if ($item['type'] === 'horoscope_analysis_info')
                    {
                        $item['target'] = 'link';
                        $item['link'] = Env::get('host.domain') . '/web/astro_explain_new/index.html?need_auth=1';
                    }
                    if ($item['type'] === 'divination_question')
                    {
                        $item['link'] = 'divination_question?type=0&name1=%E5%8D%A0%E6%98%9F%E9%AA%B0%E5%AD%90&need_auth=0';
                    }
                    $bt[] = $item;
                }
            }
            $resBtn2 = $bt;
        } else
        {
            $bt = [];
            foreach ($resBtn2 as $item)
            {
                if ($item['type'] === 'fortune_day_note')
                {
                    $item['name'] = '每日笔记';
                }
                if ($item['type'] === 'day')
                {
                    $item['name'] = '每日海报';
                }
                $bt[] = $item;
            }
            $resBtn2 = $bt;
        }

        return [
            'is_live_ing' => $isLiveIng,
            'preview_info' => $preview,
            'btn1' => $resBtn1,
            'btn2' => $resBtn2,
        ];
    }
}
